package com.sunlands.zlcx.usercenter.util;

import java.math.BigDecimal;
import java.text.DecimalFormat;


/**
 * <p></p>
 * <PRE>
 * <BR>    修改记录
 * <BR>-----------------------------------------------
 * <BR>    修改日期         修改人          修改内容
 * </PRE>
 *
 * @author zl
 * @version 1.0
 * @date Created in 2018/12/19 16:36
 * @copyright: Copyright (c) founders
 */

public class DataFormat {

    private DataFormat() {
    }


    /**
     * 对当前数 保存两位小数
     *
     * @param number
     * @return
     */
    public static String formatToTwoDecimals(double number) {
        DecimalFormat format = new DecimalFormat();
        format.applyPattern("####0.##");
        return format.format(number);
    }

    /**
     * 对当前数求百分比 保存两位小数
     *
     * @param number
     * @return
     */
    public static String formatToTwoDecimalsPercent(double number) {
        number = number * 100;
        DecimalFormat format = new DecimalFormat();
        format.applyPattern("####0.##");
        return format.format(number) + "%";
    }

    /**
     * 对当前数求百分比 保存两位小数,不带%
     *
     * @param number
     * @return
     */
    public static double doubleToTwoDecimalsPercent(Double number) {
        return doubleToScaleDecimalsPercent(number, 2);
    }

    /**
     * 对当前数求百分比 保存scale位小数,不带%
     *
     * @param number
     * @param scale
     * @return
     */
    public static double doubleToScaleDecimalsPercent(Double number, int scale) {
        if (number == null) {
            number = 0D;
            return number;
        }

        number = number * 100;
        BigDecimal bigDecimal = new BigDecimal(number);
        return bigDecimal.setScale(scale, BigDecimal.ROUND_HALF_UP).doubleValue();
    }

    /**
     * 求和
     *
     * @param v1
     * @param v2
     * @return
     */
    public static long sumLong(Long v1, Long v2) {
        if (v1 == null) {
            v1 = 0L;
        }
        if (v2 == null) {
            v2 = 0L;
        }
        return v1 + v2;
    }

    /**
     * v1减去v2
     *
     * @param v1
     * @param v2
     * @return
     */
    public static long decrementLong(Long v1, Long v2) {
        if (v1 == null) {
            v1 = 0L;
        }
        if (v2 == null) {
            v2 = 0L;
        }
        return v1 - v2;
    }

    /**
     * v1除以v2保留scale位小数
     *
     * @param v1
     * @param v2
     * @return
     */
    public static double divideLongScaleDecimals(Long v1, Long v2, int scale) {
        if (v1 == null) {
            v1 = 0L;
        }
        if (v2 == null) {
            v2 = 0L;
        }
        if (v2 == 0) {
            return 0;
        }

        return divideDoubleScaleDecimals(Double.valueOf(v1), Double.valueOf(v2), scale);
    }

    /**
     * v1除以v2保留2位小数
     *
     * @param v1
     * @param v2
     * @return
     */
    public static double divideLongTwoDecimals(Long v1, Long v2) {
        return divideLongScaleDecimals(v1, v2, 2);
    }

    /**
     * v1除以v2保留4位小数
     *
     * @param v1
     * @param v2
     * @return
     */
    public static double divideLongFourDecimals(Long v1, Long v2) {
        return divideLongScaleDecimals(v1, v2, 4);
    }

    /**
     * v1除以v2保留scale位小数
     *
     * @param v1
     * @param v2
     * @return
     */
    public static double divideDoubleScaleDecimals(Double v1, Double v2, int scale) {
        if (v1 == null) {
            v1 = 0D;
        }
        if (v2 == null) {
            v2 = 0D;
        }
        if (v2 == 0) {
            return 0;
        }

        BigDecimal d1 = new BigDecimal(v1);
        BigDecimal d2 = new BigDecimal(v2);
        return d1.divide(d2, scale, BigDecimal.ROUND_HALF_UP).doubleValue();

    }

    /**
     * v1除以v2保留4位小数
     *
     * @param v1
     * @param v2
     * @return
     */
    public static double divideDoubleFourDecimals(Double v1, Double v2) {
        return divideDoubleScaleDecimals(v1, v2, 4);
    }

    /**
     * v1除以v2保留2位小数
     *
     * @param v1
     * @param v2
     * @return
     */
    public static double divideDoubleTwoDecimals(Double v1, Double v2) {
        return divideDoubleScaleDecimals(v1, v2, 2);
    }

    /**
     * 毫秒转秒，保留2位小数
     *
     * @param millisecond
     * @return
     */
    public static double millisecond2SecondTwoDecimals(long millisecond) {
        return divideLongTwoDecimals(millisecond, 1000L);
    }

    public static double sumDoubleScaleDecimals(Double v1, Double v2, int scale) {
        if (v1 == null) {
            v1 = 0D;
        }
        if (v2 == null) {
            v2 = 0D;
        }

        BigDecimal d1 = new BigDecimal(v1);
        BigDecimal d2 = new BigDecimal(v2);
        return d1.add(d2).setScale(scale, BigDecimal.ROUND_HALF_UP).doubleValue();

    }

    public static double sumDoubleTwoDecimals(Double v1, Double v2) {
        return sumDoubleScaleDecimals(v1, v2, 2);
    }

    /**
     * 1:20:15
     *
     * @param second
     * @return
     */
    public static String getTimeStr(long second) {
        if (second <= 0) {
            return "00:00";
        }
        StringBuilder sb = new StringBuilder("");
        long day = second / (24 * 60 * 60);
        long hour = (second / (60 * 60) - day * 24);
        long min = ((second / (60)) - day * 24 * 60 - hour * 60);
        long s = (second - day * 24 * 60 * 60 - hour * 60 * 60 - min * 60);
        if (day != 0) {
            sb.append(day).append("天 ");
        }
        if (hour != 0) {
            sb.append(hour).append(":");
        }
        if (min < 10) {
            sb.append("0");
        }
        sb.append(min).append(":");
        if (s < 10) {
            sb.append("0");
        }
        sb.append(s);

        return sb.toString();
    }

    public static void main(String[] args) {

    }


}
